<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="优惠券名称">
				<el-input v-model="queryParams.couponName" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态">
				<el-select v-model="queryParams.status" clearable>
					<el-option label="正常" value="0"></el-option>
					<el-option label="禁用" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"><el-icon> <ele-Search /> </el-icon>搜索</el-button>
				<el-button @click="resetQuery"><el-icon> <ele-Refresh /> </el-icon>重置</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" @click="add" v-auth="'mall:couponinfo:add'"><el-icon> <ele-Plus />
					</el-icon>新增</el-button>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch"
				@refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData" border>
			<el-table-column prop="couponName" label="优惠券名称" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="picUrl" label="有效时间" align="center" show-overflow-tooltip>
				<template #default="scope">
					{{ scope.row.receiveStartedAt }}-
					{{ scope.row.receiveEndedAt }}
				</template>
			</el-table-column>

			<el-table-column prop="couponType" label="优惠形式" align="center">
				<template #default="scope">
					<span v-if="scope.row.couponType == '1'" type="danger"> 指定金额:{{ scope.row.amount }}元 </span>
					<span v-if="scope.row.couponType == '2'" type="success">{{ scope.row.discount }}折</span>
				</template>
			</el-table-column>
			<el-table-column prop="useRange" label="可用范围" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.useRange == '1'" type="success">全部商品</el-tag>
					<el-tag v-if="scope.row.useRange == '2'" type="danger">指定商品</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="totalNum" label="发行数量" align="center"></el-table-column>
			<el-table-column prop="assignCount" label="已发放数量" align="center"></el-table-column>
			<el-table-column prop="usedCount" label="已使用数量" align="center"></el-table-column>
			<el-table-column prop="remainNum" label="剩余数量" align="center"></el-table-column>
			<el-table-column prop="status" label="状态" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.status == '0'" type="success">正常</el-tag>
					<el-tag v-if="scope.row.status == '1'" type="danger">禁用</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180" />
			<el-table-column label="操作" width="200" align="center" fixed="right">
				<template #default="scope">
					<el-button link type="primary" @click="edit(scope.row)" v-auth="'mall:couponinfo:edit'"><el-icon>
							<ele-Edit /> </el-icon>修改</el-button>
					<el-button link type="danger" @click="del(scope.row.id)" v-auth="'mall:couponinfo:del'"><el-icon>
							<ele-Delete /> </el-icon>删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination v-model:total="page.total" v-model:currentPage="page.currentPage" v-model:pageSize="page.pageSize"
			@changePage="initPage"></pagination>
	</main-container>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { getPage, delObj } from '/@/api/mall/couponinfo';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
export default defineComponent({
	setup() {
		const data = reactive({
			queryParams: {
				couponName: '',
				status: '',
			},
			page: {
				total: 0,
				currentPage: 1,
				pageSize: 10,
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);
		const $route = useRouter();

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				descs: data.page.descs,
			};
			await getPage(Object.assign(params, data.queryParams))
				.then((response) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = { couponName: '', status: '' };
		};
		/**
		 * 新增按钮
		 */
		const add = () => {
			$route.push({ path: '/coupon/form' });
		};
		/**
		 * 修改按钮
		 */
		const edit = (row: any) => {
			$route.push({ path: '/coupon/form', query: { id: row.id } });
		};
		/**
		 * 删除按钮
		 */
		const del = (id: string) => {
			ElMessageBox.confirm('此操作将删除该优惠券，是否继续?', '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delObj(id)
					.then((response) => {
						ElMessage.success('删除成功');
						initPage();
					})
					.catch(() => { });
			});
		};
		initPage();
		return {
			loading,
			showSearch,
			del,
			add,
			edit,
			initPage,
			resetQuery,
			...toRefs(data),
		};
	},
});
</script>
